import { createRouter, createWebHistory } from "vue-router";
import { ElMessage } from 'element-plus'
import HomeView from "../views/HomeView.vue";
import LoginView from '../views/LoginView.vue';
import RoleSelectView from '../views/RoleSelectView.vue'

import FullyConnectedNeuralNetworkView from "../views/models/FullyConnectedNeuralNetworkView.vue"
import AnnouncementView from '../views/AnnouncementView.vue'

import StudentManageMentView from "../views/student/StudentManagementView.vue"

import ProfileView from "../views/ProfileView.vue";

import ExperimentListView from "../views/ExperimentListView.vue";
import ExperimentDetailView from "../views/ExperimentDetailView.vue";
import TaskDetailView from "../views/TaskDetailView.vue";

import TeacherDashboardView from "../views/teacher/TeacherDashboardView.vue";
import TeacherExperimentsView from "../views/teacher/TeacherExperimentsView.vue";
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/',
     name: 'announcement', 
     component: AnnouncementView },
    { path: '/role-select',
       name: 'role-select', 
       component: RoleSelectView },
  { path: '/login', 
    name: 'login',
     component: LoginView },
    {
      path: "/home",
      name: "home",
      component: HomeView,
    },
    {
      path: "/FCNN",
      name: "fully-connected-neural-network",
      component: FullyConnectedNeuralNetworkView,
    },
    {
      path: "/student/management",
      name: "student-management",
      component: StudentManageMentView,
      meta: { 
        requiresAuth: true ,
        // requiredRole: 'teacher' // 仅教师可访问

      }
    },
    {
      path: "/profile",
      name: "profile",
      component: ProfileView,
      meta: { requiresAuth: true }
    },
    {
      path: "/experiments",
      name: "experiment-list",
      component: ExperimentListView,
      meta: { requiresAuth: true }
    },
    {
      path: '/experiments/:id',
      name: 'experiment-detail',
      component: ExperimentDetailView,
      meta: { requiresAuth: true },
      props: true
    },
    {
      path: '/experiments/:expId/tasks/:taskId/record/:recordId',
      name: 'task-detail',
      component: TaskDetailView,
      meta: { requiresAuth: true },
      props: true
    },
     {
      path: '/teacher/dashboard',
      name: 'teacher-dashboard',
      component: TeacherDashboardView,
      meta: { 
        requiresAuth: true,
        //requiredRole: 'teacher'
      }
    },
    {
      path: '/teacher/experiments',
      name: 'teacher-experiments',
      component: TeacherExperimentsView,
      meta: { 
        requiresAuth: true,
        //requiredRole: 'teacher'
      }
    },
    {
      path: '/experiment-records',
      name: 'StudentExperimentRecordView',
      component: () => import('@/views/student/StudentExperimentRecordView.vue'),
      meta: { requiresAuth: true, role: 'student' }
    },
    {
      path: '/code/edit',
      name: 'code-edit',
      component: () => import('@/views/code/EditBoard.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/code/edit/:questionId',
      name: 'code-edit-id',
      component: () => import('@/views/code/EditBoard.vue'),
      meta: { requiresAuth: true },
      props: true
    },
    {
      path: '/code/list',
      name: 'code-question-list',
      component: () => import('@/views/code/CodeQuestionList.vue'),
      meta: { requiresAuth: true }
    }
  ],
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const role = localStorage.getItem('role'); // 获取用户角色
  // 需要登录但未登录
  if (to.meta.requiresAuth && !token) {
    // 如果访问实验相关页面，给出提示并回到身份选择
    if (to.name === 'experiment-list' || to.name === 'experiment-detail' || to.name === 'task-detail') {
      ElMessage.warning('请先登录后再访问实验列表！');
      next({ path: '/role-select' });
    } else {
      ElMessage.warning('请先登录！');
      next({ path: '/role-select' });
    }
  }
  // 需要特定角色但当前角色不符
  else if (to.meta.requiredRole && to.meta.requiredRole !== role) {
    ElMessage.warning('无权限访问该页面');
    next('/');
  }
  else {
    next();
  }
});

export default router;